<template>
    <a-card :bordered="false" :bodyStyle="{ padding: 0 }">
        <a-divider style="font-weight: bold;">预定分配房间</a-divider>
        <a-row :gutter="5">
            <a-col :span="8">
                <RoomList ref="refRoomList" :yudingdan="yudingdan" @selectRooms="selectRooms" />
                <example />
            </a-col>
            <a-col :span="16">
                <WithoutRoomsList ref="refWithoutRoomsList" :yudingdan="yudingdan"
                    @selectWithoutRooms="selectWithoutRooms" />
                <a-card :bodyStyle="{ padding: '5px' }">
                    <a-space>
                        <a-button icon="down-circle" type="primary" @click="handleFF"
                            :loading="ffLoading">确认分房</a-button>
                        <a-button icon="up-circle" type="danger" @click="cancleFF" :loading="ffLoading">取消分房</a-button>
                    </a-space>
                    <a-button icon="down-square" type="danger" style="float: right;" @click="autoFF"
                        :loading="ffLoading">全部自动分配</a-button>
                </a-card>
                <!-- <WithRoomsList ref="refWithRoomsList" :yudingdan="yudingdan" @selectWithRooms="selectWithRooms" /> -->
            </a-col>
        </a-row>
    </a-card>
</template>

<script>
import RoomList from './modules/RoomList'
import WithoutRoomsList from './modules/WithoutRoomsList'
// import WithRoomsList from './modules/WithRoomsList'
import { postAction } from '@/api/manage'
import example from '../../../FT/example.vue'

export default {
    name: 'YuDingFangJianList',
    components: { RoomList, WithoutRoomsList, example },
    props: {
        yudingdan: {
            type: Object,
            required: true,
        },
        disabled: {
            type: Boolean,
            default: false,
            required: false
        }
    },
    data() {
        return {
            url: {
                getWaitingRooms: '/room/roomInfo/queryUsableRoomList'
            },
            withoutRooms: [],
            withRooms: [],
            rooms: [],
            ffLoading: false,
        }
    },
    methods: {
        selectWithoutRooms(obj) {
            this.withoutRooms = obj.selectedRows
            this.$refs.refRoomList.loadData(obj.record.dateArrival, obj.record.dateDeparture, obj.record.roomTypeCode)
            console.log(1, obj)
        },
        selectWithRooms(obj) {
            this.withRooms = obj.selectedRows
        },
        selectRooms(obj) {
            this.rooms = obj.selectedRows
        },
        refreshRooms() {
            this.$refs.refRoomList.dataSource = []
            // this.$refs.refRoomList.loadData()
            this.$refs.refRoomList.selectedRowKeys = []
            this.rooms = []
            this.$refs.refWithoutRoomsList.loadData()
            this.$refs.refWithoutRoomsList.selectedRowKeys = []
            this.withoutRooms = []
            // this.$refs.refWithRoomsList.loadData()
            // this.$refs.refWithRoomsList.selectedRowKeys = []
            // this.withRooms = []
        },
        handleFF() {
            if (this.withoutRooms.length === 0) {
                this.$error({ title: '请选择【分配房间】', content: '请选择【分配房间】' })
                return false
            }
            if (this.rooms.length === 0) {
                this.$error({ title: '请选择【当前可用房间】', content: '请选择【当前可用房间】' })
                return false
            }
            const that = this
            const bookMxId = []
            that.withoutRooms.forEach(item => {
                bookMxId.push(item.id)
            })
            this.$confirm({
                title: '继续分配房间？',
                content: '我们将仅对房间类型相同的房间进行分配，继续吗？',
                onCancel() { },
                onOk() {
                    that.ffLoading = true
                    postAction('/room/roomBookRoom/addFp', {
                        bookMxIds: bookMxId.join(','),
                        roomCodeList: that.rooms,
                        bookCode: that.yudingdan.bookCode
                    }).then(res => {
                        if (res.success) {
                            that.$message.success('分配完成，请确认结果！')
                        } else {
                            that.$message.error(res.message)
                        }
                    }).finally(() => {
                        that.refreshRooms()
                        that.ffLoading = false
                    })
                },
            })
        },
        cancleFF() {
            if (this.withoutRooms.length === 0) {
                this.$error({ title: '请选择【分配房间】', content: '请选择【分配房间】' })
                return false
            }
            const that = this
            const bookMxId = []
            that.withoutRooms.forEach(item => {
                bookMxId.push(item.id)
            })
            this.$confirm({
                title: '确认取消分配？',
                content: '确认取消分配，继续吗？',
                onCancel() { },
                onOk() {
                    that.ffLoading = true
                    postAction('/room/roomBookRoom/cancelFp?bookMxIds=' + bookMxId.join(',') + '&bookCode=' + that.yudingdan.bookCode).then(res => {
                        if (res.success) {
                            that.$message.success('取消分配成功!')
                        } else {
                            that.$message.error(res.message)
                        }
                    }).finally(() => {
                        that.refreshRooms()
                        that.ffLoading = false
                    })
                },
            })
        },
        // cancleFF() {
        //     if (this.withRooms.length === 0) {
        //         this.$error({ title: '请选择【已分配房间】', content: '请选择【已分配房间】' })
        //         return false
        //     }
        //     const that = this
        //     const bookMxId = []
        //     that.withRooms.forEach(item => {
        //         bookMxId.push(item.id)
        //     })
        //     this.$confirm({
        //         title: '确认取消分配？',
        //         content: '确认取消分配，继续吗？',
        //         onCancel() { },
        //         onOk() {
        //             that.ffLoading = true
        //             postAction('/room/roomBookRoom/cancelFp?bookMxIds=' + bookMxId.join(',') + '&bookCode=' + that.yudingdan.bookCode).then(res => {
        //                 if (res.success) {
        //                     that.$message.success('取消分配成功!')
        //                 } else {
        //                     that.$message.error(res.message)
        //                 }
        //             }).finally(() => {
        //                 that.refreshRooms()
        //                 that.ffLoading = false
        //             })
        //         },
        //     })
        // },
        autoFF() {
            // if (this.withoutRooms.length === 0) {
            //     this.$error({ title: '请选择【未分配房间】', content: '请选择【未分配房间】' })
            //     return false
            // }
            const that = this
            const bookMxId = []
            that.withoutRooms.forEach(item => {
                bookMxId.push(item.id)
            })
            this.$confirm({
                title: '确认全部自动分配？',
                content: '继续执行将所有未分配房间进行自动分配，继续吗？',
                onCancel() { },
                onOk() {
                    that.ffLoading = true
                    // /room/roomBookRoom/autoFpRoom?bookMxIds=
                    postAction('/room/roomBookRoom/autoFpRoomByBookCode?bookCodes=' + that.yudingdan.bookCode).then(res => {
                        if (res.success) {
                            that.$message.success('自动分配成功！')
                        } else {
                            that.$message.error(res.message)
                        }
                    }).finally(() => {
                        that.refreshRooms()
                        that.ffLoading = false
                    })
                },
            })
        },
    }
}

</script>